package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

/**
 * Created by yanxing on 16/2/19.
 */
public class NLetter extends BaseLetter {
    private int mFv;
    private int mSv;
    private Paint mPaint;
    private Path mPath;
    private float mMoveX;
    private float mMoveY;
    private int mCurValue;
    private boolean isStart = false;
    private RectFloat mRectF;
    // 描绘时候的落笔点,也就是圆的半径
    public final static int SHIFT = 40;
    public final static int STROKE_WIDTH = 20;
    public final static int WIDTH = STROKE_WIDTH / 2 + SHIFT;
    public final static int LENGTH = 120;
    // n两个脚的高度
    public final static int LEG_LENGTH = LENGTH - SHIFT - STROKE_WIDTH / 2;
    // 判断是否进入了顶部弧线绘制
    private boolean isInRoundDraw = false;

    public NLetter(float x, float y) {
        super(x, y);
        // 将坐标点调整为中心点
        mCurY += LENGTH / 2;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mPath = new Path();
        mFv = mDuration / 3;
        mSv = mDuration * 2 / 3;
        // 移动到起始位置
        mMoveX = mCurX - SHIFT;
        mMoveY = mCurY;
        mPath.moveTo(mMoveX, mMoveY);
        mRectF = new RectFloat();
        mRectF.modify(mCurX - SHIFT, mCurY - SHIFT - LEG_LENGTH, mCurX + SHIFT, mCurY + SHIFT - LEG_LENGTH);
    }

    @Override
    public void reset() {
        super.reset();

        mCurY += LENGTH / 2;
        if (mPaint != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.STROKE_STYLE);
            mPaint.setStrokeWidth(STROKE_WIDTH);
        }

        mFv = mDuration / 3;
        mSv = mDuration * 2 / 3;
        // 移动到起始位置
        mMoveX = mCurX - SHIFT;
        mMoveY = mCurY;
        if (mPath != null) {
            mPath.reset();
            mPath.moveTo(mMoveX, mMoveY);
        }
        if (mRectF != null) {
            mRectF.modify(mCurX - SHIFT, mCurY - SHIFT - LEG_LENGTH, mCurX + SHIFT, mCurY + SHIFT - LEG_LENGTH);
        }
        isStart = false;
        isInRoundDraw = false;
    }

    @Override
    public void startAnim() {
        AnimatorValue animator = new AnimatorValue();
        animator.setCurveType(Animator.CurveType.LINEAR);
        animator.setDuration(mDuration);
        animator.setValueUpdateListener((ani, v) -> {
            if (!isStart) {
                return;
            }
            mCurValue = (int) (1 + (mDuration - 1) * v);
            if (mCurValue <= mFv) {
                mMoveY = mCurY - LEG_LENGTH * mCurValue / mFv;
                mPath.lineTo(mMoveX, mMoveY);
            } else if (mCurValue <= mSv) {
                if (!isInRoundDraw) {
                    isInRoundDraw = true;
                    mPath.lineTo(mMoveX, mCurY - LEG_LENGTH);
                }
                mCurValue -= mFv;
                mPath.addArc(mRectF, 180, mCurValue * 180 / (mSv - mFv));
            } else {
                mCurValue -= mSv;
                mMoveX = mCurX + SHIFT;
                mMoveY = mCurY - LEG_LENGTH + LEG_LENGTH * mCurValue / (mDuration - mSv);
                mPath.lineTo(mMoveX, mMoveY);
            }
        });
        animator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onStart(Animator animator) {
                super.onStart(animator);
                isStart = true;
            }

            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });
        animator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        if (isStart) {
            canvas.drawPath(mPath, mPaint);
        }
    }
}
